﻿using ScottPlot.Interactivity;
using SkiaSharp;
using System;
using System.Runtime.CompilerServices;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Dotnet.ScottPlotEx
{
    /// <summary>
    /// 
    /// </summary>
    unsafe internal class GreatWaterfallPlot2
    {
        private readonly SKPaint _paint;


        private int _framCount = 0;
        private int _fftLength = 0;
        private int _plotWidth = 0;
        private int _plotHeight = 0;


        /// <summary>
        /// 
        /// </summary>
        public GreatWaterfallPlot2()
        {

        }


        private SKBitmap _fullBitmap = null;
        private SKImageInfo _imageInfo;
        private SKBitmap _bitmap2 = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="specData"></param>
        /// <param name="fftLength"></param>
        /// <param name="plotWidth"></param>
        /// <param name="plotHeight"></param>
        public void AppendDatas(SpanZ<double> specData, int fftLength, int plotWidth, int plotHeight)
        {
            this._fftLength = fftLength;
            this._framCount = specData.Length / fftLength;
            this._plotWidth = plotWidth;
            this._plotHeight = plotHeight;

            using (var argbsPointer = new UnmanagedMemoryPointer<uint>(fftLength * this._framCount))
            {
                //todo...

                this._fullBitmap?.Dispose();
                this._imageInfo = new SKImageInfo(fftLength, this._framCount);
                this._fullBitmap = new SKBitmap(this._imageInfo);
                this._fullBitmap.InstallPixels(this._imageInfo, new IntPtr(argbsPointer.DataPtr), this._imageInfo.RowBytes);
            }

            this._bitmap2 = new SKBitmap(plotWidth, plotHeight);
            this._fullBitmap.ScalePixels(this._bitmap2, SKFilterQuality.High);//缩放
        }

        public void MouseWheel(int delta)
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="surface"></param>
        public void Render(SKSurface surface)
        {
            surface.Canvas.DrawBitmap(this._bitmap2, 0f, 0f, this._paint);
            //RenderManager.Render(surface.Canvas, surface.Canvas.LocalClipBounds.ToPixelRect());
        }

    }
}
